package cn.itcast.mq.rabbitMqTest;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

/**
 *生产消息队列
 * @Author mr.wu
 * @Date 2025/1/5 11:10
 */
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void test(){
        rabbitTemplate.convertAndSend("simple.queue","我真牛逼");
    }

    /**
     * WorkQueue 工作队列模型
     * @Author mr.wu
     * @Date 2025/1/5 11:11
     */

    @Test
    public void test2(){
        String msg = "工作队列模型";
        for (int i = 0; i < 100; i++) {
        rabbitTemplate.convertAndSend("Work.queue",msg+i);
        }
    }

    /**
     * 广播模型
     */

    @Test
    public void teatFanout(){
        rabbitTemplate.convertAndSend("fanout.exchange","" ,"广播模型");
    }

    /**
     * 路由模型-生产消息
     */
    @Test
    public void testRouting(){
        rabbitTemplate.convertAndSend("direct.exchange","blue","路由模型-blue");
        rabbitTemplate.convertAndSend("direct.exchange","red","路由模型-red");
    }

    /**
     * Topic主题模型
     */
    @Test
    public void testTopic(){
        rabbitTemplate.convertAndSend("topic.exchange","china","msg 1");
        rabbitTemplate.convertAndSend("topic.exchange","china.news","msg 2");
        rabbitTemplate.convertAndSend("topic.exchange","news.china","msg 3");
        rabbitTemplate.convertAndSend("topic.exchange","news.china.news","msg 4");
        rabbitTemplate.convertAndSend("topic.exchange","news.japan.china","msg 5");
    }

    /**
     * 消息转换器
     */

    //.测试默认转换器
    @Test
    public void testSendMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",18);
        rabbitTemplate.convertAndSend("object.queue",map);
    }

}
